/**
* @file        NETWORK.D3.h
*
* @ingroup     libNetwork	
* @brief	D3: Data-centric Data Dissemination routing
* 
* @author	Andreas Schlueter <mersaultpatrice@gmail.com>
* @date	Mar 2008
* @version	0.1
*/

#ifndef NETWORK_D3_H
#define NETWORK_D3_H

#include "ScatterWeb.Net.h"
#include "..\include\Network.DLL.h"

///< maximal(network wide) number of packets in memory
#define MAX_PACKETS 16

/**
* debug level. information is printed to serial
* */
//#define D3_DEBUG_PROTOCOL 1 ///< protocol msg communication
#define D3_DEBUG    1 ///< error details

/**
* INT packet (17bytes frame) 
*/
__attribute__((packed)) typedef struct{
	uint8_t depth;       ///< hopcounter
	uint8_t sequence_nr; ///< difference to expired INT packets
#ifdef D3_OFFSET
	int slot_correction;	///< correction for slot time computation 
#endif
} IntPacket;
#define TX_INT_TIME 525 ///< ~525ticks per TX by 8.5kbps

/**
* TIME packet (transmitted immediately after ADV)
*/
__attribute__((packed)) typedef struct{  
	//uint8_t  sequence_nr; ///< msg identifier
	uint16_t start_delay;  ///< duration (ms) to next DAT slot of sender relative to time_rx_adv
	uint8_t  cost;         ///< cost of msg sender
} TimePacket;

/**
* ADV packet (also implicit acknowledgement)
*/
__attribute__((packed)) typedef struct{
	segment_key_t list[MAX_PACKETS];  ///< list of max. 4 DAT packets
	uint8_t cost;    ///< cost of msg sender
	uint8_t depth;   ///< hopcounter from msg originator
} AdvPacket;



/**
* EACK packet (explicit acknowledgement)
*/
__attribute__((packed)) typedef struct{
	segment_key_t list[MAX_PACKETS];  ///< identifies DAT packets of already received packets
	uint8_t cost;    ///< cost of msg sender
} EAckPacket;

//#ifdef D3_TEST
/**
* sets the cost for test cases
*/
__attribute__((packed)) typedef struct{
	uint8_t destID;
	uint8_t cost;    ///< cost of msg sender
} CostPacket;
//#endif

/**
* @brief information packet 
* 
* shall have an even number of uint8_t
*/
__attribute__((packed)) typedef struct{
	//node information
	uint8_t  node_type;      //b4 
	uint8_t  cost;  
	uint16_t neighbours;      //b6-7
	uint16_t num_sd_paging;

	//packtet statistic
	uint16_t  num_rx_dat;  //b10-11
	uint16_t  num_rx_adv;  //b12-13
	uint16_t  num_rx_time;        
	uint16_t  num_rx_vAck; //b16-17
	uint16_t  num_rx_eAck;

	uint16_t  num_tx_dat; //b20-21
	uint16_t  num_tx_adv;
	uint16_t  num_tx_eAck;
	uint16_t  num_lost_races;
	uint16_t  num_retransmissions;

	//errors and warnings
	uint16_t  num_errors; //b30-31
	uint16_t  num_warnings;
	uint8_t   last_error; //b34
	uint8_t   last_warning; //b35

	//time statistics
	uint16_t num_waitcycles; //b36-37
	int child_time_offset_max;
	uint16_t stime_last_tx_adv;
	uint16_t stime_last_tx_time;
	uint16_t stime_last_tx_eack;
	uint16_t stime_last_tx_dat;
	uint16_t stime_last_rx_adv;
	uint16_t stime_last_rx_time;
	uint16_t stime_last_rx_eack;
	uint16_t stime_last_rx_dat;
	uint16_t stime_max_rx_adv;
	uint16_t stime_max_rx_time;
	uint16_t stime_max_rx_eack;
	uint16_t stime_max_rx_dat;
	uint16_t stime_min_rx_adv;
	uint16_t stime_min_rx_time;
	uint16_t stime_min_rx_eack;
	uint16_t stime_min_rx_dat;

	uint16_t time_adv_slot;
	uint16_t time_dat_slot;
	uint16_t time_last_rx_adv;
	uint16_t time_last_delay;
} InfoPacket;



/**
* @brief initializes the usage of d3 routing
*
* @return 0 if success
*/
uint8_t d3_init();



#endif


